package com.vcc.web.core.component;

import org.springframework.stereotype.Component;

@Component
public class PingPongComponent {
//
//    private static final Logger logger = LoggerFactory.getLogger(PingPongComponent.class);
//
//    @Autowired
//    private PingPongConfig pingPongConfig;
//    @Autowired
//    private WalletDetailService walletDetailService;
//    @Autowired
//    private WalletService walletService;
//    @Autowired
//    private CurrencyService currencyService;
//    @Autowired
//    private UserCardWalletService userCardWalletService;
//    @Autowired
//    private RestTemplate restTemplate;
//    private final LoadingCache<String, String> graphs = Caffeine.newBuilder()
//            //过期时间
//            .expireAfterWrite(59, TimeUnit.MINUTES)
//            .build(key -> token());
//    @Autowired
//    private CardService cardService;
//    @Autowired
//    private PingPongIssuingOpenApiV2Api apiInstance;
//
//    public String token() {
//        String url = pingPongConfig.getUrl() + "/v2/token/get?app_id=" + pingPongConfig.getAppId() + "&app_secret=" + pingPongConfig.getAppSecret();
//        JSONObject result = getForBaseMap(url);
//        if (result.getIntValue("code") == 0) {
//            JSONObject dataMap = result.getJSONObject("data");
//            return dataMap.getString("access_token");
//        } else {
//            throw new ProgramException(result.getString("message"));
//        }
//    }
//
//    public String retrieve() {
//        return graphs.get("TOKEN");
//    }
//
//    private JSONObject getForBaseMap(String url) {
//        JSONObject resultMap = new JSONObject();
//        try {
//            HttpHeaders headers = new HttpHeaders();
//            headers.add("Content-Type", "application/json");
//            ResponseEntity<String> response =
//                    restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<String>(headers), String.class);
//            resultMap = JSONObject.parseObject(response.getBody());
//            if (RegexUtil.checkObjectIsNull(resultMap)) {
//                throw new ProgramException("系统异常");
//            }
//        } catch (Exception e) {
//            resultMap.put("code", 999);
//        }
//        return resultMap;
//    }
//
//
//    public String openCard(String orderId, Card card, Wallet wallet, User user, String remark, BigDecimal amount) {
//        ApplyCardRequest request = new ApplyCardRequest();
//        request.setCardProductCode(card.getCode());
//        request.setRequestId(orderId);
//        request.setUseECard(true);
//        try {
//            ApiResponseOfstring result = apiInstance.applyCardUsingPOST(retrieve(), request);
//            if (result.getCode() == 6666) {
//                BigDecimal openCardResultAmount = walletService.subtractUsableQuotaById(wallet.getId(), card.getAmount(), 2, false);
//                walletDetailService.openCard("", orderId, remark, card.getAmount(), openCardResultAmount, 2, wallet.getCurrencyId(), user.getId());
//                return result.getData();
//            } else {
//                logger.error("开卡失败，请求参数{},返回结果{}", card.getCode(), result.toString());
//                throw new ProgramException("开卡失败");
//            }
//        } catch (Exception e) {
//            BigDecimal openCardResultAmount = walletService.addUsableQuotaById(wallet.getId(), card.getAmount(), 4, false);
//            walletDetailService.updateStatusAndWalletAmountByOrderId(1, BigDecimal.valueOf(0), orderId);
//            logger.error("开卡失败，失败原因", e);
//            throw new ProgramException("开卡失败");
//        }
//    }
//
//    public String openCard(String code, String orderId) {
//        ApplyCardRequest request = new ApplyCardRequest();
//        request.setCardProductCode(code);
//        request.setRequestId(orderId);
//        request.setUseECard(true);
//        try {
//            ApiResponseOfstring result = apiInstance.applyCardUsingPOST(retrieve(), request);
//            if (result.getCode() == 6666) {
//                return result.getData();
//            } else {
//                logger.error("储备卡失败，失败原因{}", result.toString());
//                return "";
//            }
//        } catch (Exception e) {
//            logger.error("储备卡失败，失败原因", e);
//            return "";
//        }
//    }
//
//    public CardDetail2 getCardInfo(String ppCardId) {
//        try {
//            ApiResponseOfCardDetail2 apiResponseOfCardDetail2 = apiInstance.queryAccountBalance2UsingGET(retrieve(), ppCardId);
//            if (apiResponseOfCardDetail2.getCode() == 6666) {
//                return apiResponseOfCardDetail2.getData();
//            } else {
//                logger.error("获取卡信息失败，失败原因{}", apiResponseOfCardDetail2.toString());
//                throw new ProgramException("获取卡信息失败");
//            }
//        } catch (ApiException e) {
//            logger.error("获取卡信息失败，失败原因", e);
//            throw new ProgramException("获取信息失败");
//        }
//    }
//
//    public String recharge(String orderId, String remark, UserCard userCard, BigDecimal amount) {
//        Wallet wallet = walletService.getByUserIdAndCurrencyCode(userCard.getUserId(), userCard.getCurrencyCode());
//        UserCardWallet userCardWallet = userCardWalletService.findByUserIdAndCardId(userCard.getUserId(), userCard.getCardId());
//        CardInRequest request = new CardInRequest();
//        request.setAmount(amount.toString());
//        request.setPpCardId(userCard.getChannelCardId());
//        request.setUniqueOrderId(orderId);
//        BigDecimal rechargeResultAmount = null;
//        try {
//            //优先扣费  扣费成功在去充值
//            rechargeResultAmount = walletService.subtractUsableQuotaById(wallet.getId(), amount, 2, false);
//            walletDetailService.recharge(userCard.getCardNumber(), orderId, remark, amount, rechargeResultAmount, 0, wallet.getCurrencyId(), wallet.getUserId());
//            ApiResponseOfstring apiResponseOfstring = apiInstance.rechargeUsingPOST(retrieve(), request);
//            //充值成功
//            if (apiResponseOfstring.getCode() == 6666) {
//                return apiResponseOfstring.getData();
//            } else {
//                if (rechargeResultAmount != null) {
//                    walletService.addUsableQuotaById(wallet.getId(), amount, 4, false);
//                }
//                walletDetailService.updateStatusAndWalletAmountByOrderId(1, BigDecimal.valueOf(0), orderId);
//                logger.error("充值失败，失败原因{}", apiResponseOfstring.toString());
//                throw new ProgramException("卡充值失败");
//            }
//        } catch (Exception e) {
//            if (rechargeResultAmount != null) {
//                walletService.addUsableQuotaById(wallet.getId(), amount, 4, false);
//            }
//            walletDetailService.updateStatusAndWalletAmountByOrderId(1, BigDecimal.valueOf(0), orderId);
//            logger.error("充值失败，失败原因", e);
//            throw new ProgramException("充值失败");
//        }
//    }
//
//    public String out(String orderId, UserCard userCard, BigDecimal amount, String remark) {
//        Wallet wallet = walletService.getByUserIdAndCurrencyCode(userCard.getUserId(), userCard.getCurrencyCode());
//        CardOutRequest request = new CardOutRequest();
//        request.setAmount(amount.toString());
//        request.setPpCardId(userCard.getChannelCardId());
//        request.setUniqueOrderId(orderId);
//        try {
//            ApiResponseOfstring apiResponseOfstring = apiInstance.chargeOutUsingPOST(retrieve(), request);
//            if (apiResponseOfstring.getCode() == 6666) {
//                walletDetailService.out(userCard.getCardNumber(), orderId, remark, amount, BigDecimal.valueOf(0), 2, wallet.getCurrencyId(), userCard.getUserId());
//                return apiResponseOfstring.getData();
//            } else {
//                logger.error("转出失败，失败原因{}", apiResponseOfstring.toString());
//                throw new ProgramException("转出失败");
//            }
//        } catch (ApiException e) {
//            logger.error("转出失败，失败原因", e);
//            throw new ProgramException("转出失败");
//        }
//    }
//
//    public boolean block(UserCard userCard) {
//        CardBlockRequest request = new CardBlockRequest();
//        request.setPpCardId(userCard.getChannelCardId());
//        try {
//            ApiResponseOfVoid apiResponseOfVoid = apiInstance.blockUsingPOST(retrieve(), request);
//            if (apiResponseOfVoid.getCode() == 6666) {
//                walletDetailService.freeze(userCard.getCardNumber(), IdWorker.getStringOrderId(), "", BigDecimal.valueOf(0), BigDecimal.valueOf(0), 0, 0L, userCard.getUserId());
//                return true;
//            } else {
//                logger.error("获取卡信息失败，失败原因{}", apiResponseOfVoid.toString());
//                throw new ProgramException("冻结失败");
//            }
//        } catch (ApiException e) {
//            logger.error("冻结失败，失败原因", e);
//            throw new ProgramException("冻结失败");
//        }
//    }
//
//    public boolean unBlock(UserCard userCard) {
//        CardUnBlockRequest request = new CardUnBlockRequest();
//        request.setPpCardId(userCard.getChannelCardId());
//        try {
//            ApiResponseOfVoid apiResponseOfVoid = apiInstance.unblockUsingPOST(retrieve(), request);
//            if (apiResponseOfVoid.getCode() == 6666) {
//                walletDetailService.unFreeze(userCard.getCardNumber(), IdWorker.getStringOrderId(), "", BigDecimal.valueOf(0), BigDecimal.valueOf(0), 0, 0L, userCard.getUserId());
//                return true;
//            } else {
//                logger.error("解冻失败，失败原因{}", apiResponseOfVoid.toString());
//                throw new ProgramException("解冻失败");
//            }
//        } catch (ApiException e) {
//            logger.error("解冻失败，失败原因", e);
//            throw new ProgramException("解冻失败");
//        }
//    }
//
//    public boolean close(UserCard userCard) {
//        Wallet wallet = walletService.getByUserIdAndCurrencyCode(userCard.getUserId(), userCard.getCurrencyCode());
//        CardData apiResponseOfCardData = queryCardBalance(userCard.getChannelCardId());
//        if (Objects.equals(new BigDecimal(apiResponseOfCardData.getAvailableBalance()).compareTo(BigDecimal.valueOf(0)), -1)) {
//            throw new ProgramException("当前余额小于0，无法注销");
//        }
//        if (Objects.equals(new BigDecimal(apiResponseOfCardData.getAvailableBalance()).compareTo(BigDecimal.valueOf(0)), 1)) {
//            throw new ProgramException("当前卡存在余额，无法注销，请优先转出余额在操作");
//        }
//        ApplyForCancelRequest request = new ApplyForCancelRequest();
//        request.setPpCardId(userCard.getChannelCardId());
//        try {
//            ApiResponseOfstring apiResponseOfstring = apiInstance.applyForCancelUsingPOST(retrieve(), request);
//            if (apiResponseOfstring.getCode() == 6666) {
//                walletDetailService.closeCard(userCard.getCardNumber(), IdWorker.getStringOrderId(), "", BigDecimal.valueOf(0), BigDecimal.valueOf(0), 0, wallet.getCurrencyId(), userCard.getUserId());
//                return true;
//            } else {
//                logger.error("销卡失败，失败原因{}", apiResponseOfstring.toString());
//                throw new ProgramException("销卡失败");
//            }
//        } catch (ApiException e) {
//            logger.error("销卡失败，失败原因", e);
//            throw new ProgramException("销卡失败");
//        }
//    }
//
//    //getAvailableBalanceByPpCardIdUsingGET
//    public CardData queryCardBalance(String ppCardId) {
//        try {
//            ApiResponseOfCardData availableBalanceByPpCardIdUsingGET = apiInstance.getAvailableBalanceByPpCardIdUsingGET(retrieve(), ppCardId);
//            if (availableBalanceByPpCardIdUsingGET.getCode() == 6666) {
//                return availableBalanceByPpCardIdUsingGET.getData();
//            } else {
//                logger.error("查询卡余额失败，失败原因{}", availableBalanceByPpCardIdUsingGET.toString());
//                throw new ProgramException("查询卡余额失败");
//            }
//        } catch (ApiException e) {
//            logger.error("获取卡余额失败，失败原因", e);
//            throw new ProgramException("获取卡余额失败");
//        }
//    }
//
//
//    public AccountBalance queryAccountBalance() {
//        try {
//            ApiResponseOfAccountBalance apiResponseOfAccountBalance = apiInstance.queryAccountBalanceUsingGET(retrieve(), null);
//            if (apiResponseOfAccountBalance.getCode() == 6666) {
//                return apiResponseOfAccountBalance.getData();
//            } else {
//                logger.error("查询账户余额失败，失败原因{}", apiResponseOfAccountBalance.toString());
//                throw new ProgramException("查询账户余额失败");
//            }
//        } catch (ApiException e) {
//            logger.error("获取账户余额失败，失败原因", e);
//            throw new ProgramException("获取账户余额失败");
//        }
//    }
//
//    public PageDataOfClear queryClearData(String startDate, String endDate, Integer page, Integer size) {
//        try {
//            ApiResponseOfPageDataOfClear apiResponseOfPageDataOfClear = apiInstance.clearingsUsingGET(retrieve(), null, null, endDate, startDate, null, size, page, null);
//            if (apiResponseOfPageDataOfClear.getCode() == 6666) {
//                return apiResponseOfPageDataOfClear.getData();
//            } else {
//                logger.error("查询清算交易失败，失败原因{}", apiResponseOfPageDataOfClear.toString());
//                throw new ProgramException("查询清算交易失败");
//            }
//        } catch (ApiException e) {
//            logger.error("查询清算交易失败，失败原因", e);
//            throw new ProgramException("查询清算交易失败");
//        }
//    }
//
//    public PageDataOfAuth queryAuthData(String startDate, String endDate, Integer page, Integer size) {
//        try {
//            ApiResponseOfPageDataOfAuth apiResponseOfPageDataOfAuth = apiInstance.authorizationsUsingGET(retrieve(), endDate, null, size, startDate, page);
//            if (apiResponseOfPageDataOfAuth.getCode() == 6666) {
//                return apiResponseOfPageDataOfAuth.getData();
//            } else {
//                logger.error("查询授权交易失败，失败原因{}", apiResponseOfPageDataOfAuth.toString());
//                throw new ProgramException("查询授权交易失败");
//            }
//        } catch (ApiException e) {
//            logger.error("查询授权交易失败，失败原因", e);
//            throw new ProgramException("查询授权交易失败");
//        }
//
//    }
}
